﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Security.Cryptography;
using System.Net.Sockets;
using System.Net;
using System.IO;

namespace MrBrooks.Utility
{
    /// <summary>
    /// 工具集
    /// </summary>
    public static class JavaTools
    {
        #region .与JAVA数据交换.
        /// <summary>
        /// 将Boolean转为字符串
        /// </summary>
        /// <param name="flag"></param>
        /// <returns></returns>
        public static string FormatBoolean(bool flag)
        {
            return flag.ToString();
        }

        /// <summary>
        /// 将int转为字符串
        /// </summary>
        /// <param name="flag"></param>
        /// <returns></returns>
        public static string FormatInt(int number)
        {
            return number.ToString();
        }

        /// <summary>
        /// 将DateTime转为字符串
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public static string FormatDatetime(DateTime datetime)
        {
            return datetime.ToString("yyyy-MM-dd hh:mm:ss");
        }

        /// <summary>
        /// 布尔
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static bool ParseBoolean(string param)
        {
            return (!string.IsNullOrEmpty(param)) && param.Equals("true", StringComparison.OrdinalIgnoreCase);
        }

        /// <summary>
        /// 数字
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static int ParseInt(string param)
        {
            return int.Parse(param);
        }

        /// <summary>
        /// 数字
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static decimal ParseDecimal(string param)
        {
            return decimal.Parse(param);
        }

        /// <summary>
        /// 检测数据日期(yyyyMMdd)是否合法
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static bool CheckDateValid(string param)
        {
            if (!Regex.IsMatch(param, @"\d{8}")) return false;
            int day = int.Parse(param);
            DateTime dt = DateTime.MinValue;
            if (DateTime.TryParseExact(param, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out dt))
            {
                return dt.Year >= 2000 && dt.Year < 2200;
            }

            return false;
        }

        /// <summary>
        /// 解析日期字符串
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static DateTime ParseDate(string param)
        {
            DateTime dt = DateTime.MinValue;
            DateTime.TryParseExact(param, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out dt);
            return dt;
        }
        #endregion
    }


}
